package com.nt.thah.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.nt.thah.constant.SysCode;
import com.nt.thah.dao.CarbonAccountDao;
import com.nt.thah.dao.ScoreAccountDao;
import com.nt.thah.dao.UserDao;
import com.nt.thah.entity.*;
import com.nt.thah.enums.LevelEnum;
import com.nt.thah.requestparam.BaseParam;
import com.nt.thah.service.CommonService;
import com.nt.thah.service.UserService;
import com.nt.thah.utils.DateUtil;
import com.nt.thah.utils.OssUtils;
import com.nt.thah.utils.RedisUtil;
import com.nt.thah.utils.StringUtil;
import com.nt.thah.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Description: 登录服务
 * @author: ls
 * @Date: 2020/7/10 14:00
 */
@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Resource
    private CommonService commonServiceImpl;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private OssUtils ossUtils;

    @Resource
    private ScoreAccountDao scoreAccountDao;

    @Resource
    private CarbonAccountDao carbonAccountDao;

    /**
     * @Description: 获取用户列表信息
     * @Author: ls
     * @Date: 2020/08/04 14:24
     */
    @Override
    public Map<String, Object> getList(BaseParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<User> userList = userDao.getList(param);
        Map<String, Object> resultMap = commonServiceImpl.buildQryResultMap(userList);
        if (CollectionUtils.isEmpty(userList)) {
            return resultMap;
        }
        List<UserVO> voList = buildUserDetail(userList);
        resultMap.put("list", voList);
        return resultMap;
    }

    /**
     * @Description: 获取oss的的sts信息
     * @Author: ls
     * @Date: 2020/10/19 15:43
     */
    private List<UserVO> buildUserDetail(List<User> userList) {
        List<UserVO> voList = Lists.newArrayListWithCapacity(userList.size());
        for (User user : userList) {
            UserVO userVO = UserVO.builder().build();
            BeanUtils.copyProperties(user, userVO);
            ScoreAccount sa = commonServiceImpl.getScoreAccount(user.getId());
            CarbonAccount ca = commonServiceImpl.getCarbonAccount(user.getId());
            userVO.setName(StringUtils.isBlank(user.getName()) ? "低碳用户" : user.getName());
            userVO.setScore(sa == null ? 0 : sa.getTotal());
            userVO.setCarbon(ca == null ? "0" : ca.getTotal() + "克");
            userVO.setLevel(LevelEnum.getName(user.getLevel()));
            voList.add(userVO);
        }
        return voList;
    }

    /**
     * @Description: 获取oss的的sts信息
     * @Author: ls
     * @Date: 2020/10/19 15:43
     */
    @Override
    public JSONObject getOssStsInfo() {
        String redisValue = redisUtil.get(SysCode.REDIS_KEY_OSS_STS_INFO);
        if (StringUtils.isNotBlank(redisValue)) {
            return JSON.parseObject(redisValue);
        }
        JSONObject jsonObject = ossUtils.getOssStsInfo();
        redisUtil.set(SysCode.REDIS_KEY_OSS_STS_INFO, jsonObject.toJSONString(), 55, TimeUnit.MINUTES);
        return jsonObject;
    }

    /**
     * @Description: 获取统计数据
     * @Author: ls
     * @Date: 2020/10/19 15:43
     */
    @Override
    public Map<String, Object> getStatisticsData(BaseParam param) {
        Map<String, Object> resultMap = Maps.newHashMap();
        Integer score = scoreAccountDao.getTotalScore(param);
        Integer totalScore = scoreAccountDao.getTotalScore(null);
        Integer carbon = carbonAccountDao.getTotalCarbon(param);
        Integer totalCarbon = carbonAccountDao.getTotalCarbon(null);
        resultMap.put("carbon", carbon + "克");
        resultMap.put("totalCarbon", totalCarbon + "克");
        resultMap.put("score", score);
        resultMap.put("totalScore", totalScore);
        return resultMap;
    }

}